<?php

namespace app\common\model;

use think\Model;

class BehaviorRecordDetail extends Model
{
    // 表名
    protected $name = 'behavior_record_detail';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;

    protected static function init()
    {
        static::afterDelete(function ($row) {
            BehaviorRecordImage::where('record_detail_id', $row->id)->delete();
            $count = BehaviorRecordDetail::where('record_id', $row->record_id)->count();
            if (!$count) {
                BehaviorRecord::where('id', $row->record_id)->delete();
                BehaviorRecordImage::where('record_id', $row->record_id)->delete();
            }
        });
    }

    public function category()
    {
        return $this->belongsTo(BehaviorCategory::class);
    }

    public function images()
    {
        return $this->hasMany(BehaviorRecordImage::class, 'record_detail_id');
    }
}
